package com.zf.shuashua.base;

import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.Window;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModel;

import com.zf.shuashua.R;
import com.zf.shuashua.utils.NavigationBarUtils;

public abstract class BasicActivity<V extends ViewDataBinding, VM extends ViewModel> extends AppCompatActivity {
    public V binding;
    protected VM viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        initViewDataBinding();
        NavigationBarUtils.setNavigationBarColor(this, R.color.colorPrimary);
        setupView();
        initData();
    }

    private void initViewDataBinding() {
        binding = DataBindingUtil.inflate(getLayoutInflater(), getLayoutId(), null, false);
        setContentView(binding.getRoot());
        binding.setVariable(getViewModelId(), viewModel = getViewModel());
    }

    public void setupView() {
    }

    public void initData() {
    }

    public abstract int getLayoutId();

    public abstract int getViewModelId();

    public abstract VM getViewModel();

    /**
     * 初始化 Toolbar
     *
     * @param toolbar
     * @param homeAsUpEnabled
     * @param title
     */
    protected void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, String title) {
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(homeAsUpEnabled);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
